var express = require('express');
var router = express.Router();

var user = require('../models/user');
var goods = require('../models/goods');
var collect = require('../models/collect');
var chat = require('../models/chat');

/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' });
});
// 根据用户名获取用户的信息
router.post('/userInfo', function (req, res, next) {
  user.findByUsername(req.body.username, function (err, getUser) {
    if (err) {
      res.json({ status: 1, message: "获取错误", data: err });
    } else {
      res.json({ status: 0, message: "获取成功", data: getUser });
    }
  })
});
// 获取用户添加的商品
router.post('/userGoods', function (req, res, next) {
  goods.findByUsername(req.body.goodsUser, function (err, getGoods) {
    res.json({ status: 0, message: '获取成功', data: getGoods })
  })
});
// 获取商品的具体信息
router.post('/goodsInfo', function (req, res, next) {
  goods.findById(req.body.id, function (err, getGoods) {
    res.json({ status: 0, message: '获取成功', data: getGoods })
  })
});
// 搜索获得商品
router.post('/goodsSearch', function (req, res, next) {
  goods.findByGoodsname(req.body.goodsname, function (err, getSearch) {
    res.json({ status: 0, message: "获取成功", data: getSearch });
  });
});
// 获取所有的商品
router.get('/goodsList', function (req, res, next) {
  goods.findAll(function (err, getList) {
    res.json({ status: 0, message: '获取成功', data: getList });
  });
});
// 通过搜索实现模糊查询
router.post('/search', function (req, res, next){
	goods.findBySearch(req.body.goodsName,function (err, goodsList) {
		res.json({ static:0, message:'获取成功', data:goodsList })
	})
})
// 获取特定分类的商品
router.post('/goodsClassify', function (req, res, next) {
  goods.findByClassify(req.body.classify, function (err, getClassify) {
    res.json({ status: 0, message: "获取成功", data: getClassify });
  });
});
// 获取用户收藏的所有商品
router.post('/collectList', function (req, res, next) {
  collect.findByUsername(req.body.username, function (err, collectList) {
    res.json({ status: 0, message: "获取成功", data: collectList });
  })
});
// 获取用户收藏商品的id
router.post('/collect', function (req, res, next) {
  collect.findById(req.body.username, req.body._id, function (err, userCollect) {
    res.json({ status: 0, message: "获取成功", data: userCollect });
  });
});
// 通过房间名查找房间信息
router.post('/chat', function (req, res, next) {
  chat.findByRoom(req.body.chatRoom, function (err, data) {
    if (err) {
      res.json({ status: 1, message: '查找失败', data: err });
    } else {
      res.json({ status: 0, message: '查找成功', data: data });
    }
  })
})
// 获取联系人列表
router.post('/chat/user', function (req, res, next) {
  chat.findByUsername(req.body.username, function (err, data) {
    if (err) {
      res.json({ status: 1, message: '查找失败', data: err });
    } else {
      res.json({ status: 0, message: '查找成功', data: data });
    }
  })
})

module.exports = router;
